package com.resou.reader.utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ${Garrett} on 2018/9/3.
 * Contact me krouky@outlook.com
 */
public class DateUtils {
    /**
     * 把指定long 转换成 日期 再转换成String类型
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * 获取指定格式的当前时间
     *
     * @param dateFormat
     * @return
     */
    public static String getFormatDate(String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取指定时间与当前的时间差
     *
     * @param dateFormat
     * @return
     */
    public static int getDateDay2(String dateFormat) {
        try {
            long nowTime = System.currentTimeMillis();
            long date = Long.valueOf(dateFormat) - nowTime;
            int hour = (int) (date / (1000 * 60 * 60));
            int day = hour / 24;
            if (hour > day * 24) {
                return day + 1;
            } else {
                return day;
            }

        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 获取指定时间与当前的时间差
     *
     * @param dateFormat
     * @return
     */
    public static int getDateDay(long dateFormat) {
        try {
            long nowTime = System.currentTimeMillis();
            long date = dateFormat - nowTime;
            int hour = (int) (date / (1000 * 60 * 60));
            int day = hour / 24;
            if (hour > day * 24) {
                return day + 1;
            } else {
                return day;
            }

        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * I
     * 时间间隔
     *
     * @param dateFormat
     * @return
     */
    public static int getDateMin(long dateFormat) {
        try {
            if (dateFormat == 0)
                return 0;
            long nowTime = System.currentTimeMillis();
            float date = dateFormat - nowTime;
            float min = (float) (date / (1000 * 60));
            return (int) Math.round(min);

        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 获取距离现在已有多少年/月/日/小时/分钟/刚刚
     *
     * @param time
     * @return
     */
    public static String getTheDayFromNow(long time) {
        if (time == 0) {
            return "未知";
        }
        String display = "";
        int tMin = 60 * 1000;
        int tHour = 60 * tMin;
        int tDay = 24 * tHour;
        try {
            Date tDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(transferLongToDate("yyyy-MM-dd HH:mm:ss", time));
            Date today = new Date();
            SimpleDateFormat thisYearDf = new SimpleDateFormat("yyyy");
            SimpleDateFormat todayDf = new SimpleDateFormat("yyyy-MM-dd");
            Date thisYear = new Date(thisYearDf.parse(thisYearDf.format(today)).getTime());
            Date yesterday = new Date(todayDf.parse(todayDf.format(today)).getTime());
            Date beforeYes = new Date(yesterday.getTime() - tDay);
            Date beforeYes2 = new Date(beforeYes.getTime() - tDay);
            if (tDate != null) {
                @SuppressWarnings("unused")
                SimpleDateFormat halfDf = new SimpleDateFormat("MM月dd日");
                long dTime = today.getTime() - tDate.getTime();
                if (tDate.before(thisYear)) {
                    display = new SimpleDateFormat("yyyy年MM月dd日").format(tDate);
                } else {

                    if (dTime < tMin) {
                        display = "刚刚";
                        return display;
                    } else if (dTime < tHour) {
                        display = (int) Math.ceil(dTime / tMin) + "分钟以前";
                        return display;
                    } else if (dTime < tDay && tDate.after(yesterday)) {
                        display = "今天" + new SimpleDateFormat("HH:mm").format(tDate);
                        return display;
                    } else if (tDate.after(beforeYes) && tDate.before(yesterday)) {
                        display = "昨天  " + new SimpleDateFormat("HH:mm").format(tDate);
                        return display;
                    } else if (tDate.after(beforeYes2) && tDate.before(beforeYes)) {
                        display = "前天  " + new SimpleDateFormat("HH:mm").format(tDate);
                        return display;
                    } else {
                        display = "  " + new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(tDate);
                        return display;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return display;
    }

//    /**
//     * 获取距离现在已有多少年/月/日/小时/分钟/刚刚
//     *
//     * @param time
//     * @return
//     */
//    public static String getTheDayFromNow2(long time) {
//        if (time == 0) {
//            return "未知";
//        }
//        long currentTime = System.currentTimeMillis();
//        int second = (int) ((currentTime - time) / 1000);
//        int minute = (int) (second / 60);
//        int hour = (int) (minute / 60);
//        if (hour > 48 && hour < 72) {
//            return "前天" + transferLongToDate("HH:mm", time);//day + "前天";
//        } else if (hour > 24 && hour < 48) {
//            return "昨天" + transferLongToDate("HH:mm", time);//day + "昨天";
//        } else if (hour > 0 && hour < 24) {
//
//            return "今天" + transferLongToDate("HH:mm", time);//day + "今天 ";
//        } else if (minute < 60) {
//            return minute + "分钟前";
//        } else if (second < 60) {
//            return "刚刚";
//        } else {
//            return transferLongToDate("yyyy年MM月dd日  HH:mm", time);//日期+时间
//        }
//    }

    /**
     * 日期格式化
     *
     * @param date 需要格式化的日期
     * @return 返回格式化后的时间字符串
     */
    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm");
        return sdf.format(date);
    }

    public static String getDayByWeek() {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static int getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return Integer.parseInt(sdf.format(date));
    }


    /**
     * 通过标题 返回响应日期
     *
     * @param id
     * @return
     */
    public static String getVIPendTime(String id) {
        String endTime = "";
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (id) {
            case "0":
                calendar.add(calendar.DATE, 7);
                endTime = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());
                return endTime;
            case "1":
                calendar.add(calendar.DATE, 30);
                endTime = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());
                return endTime;
            case "2":
                calendar.add(calendar.DATE, 30 * 3);
                endTime = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());
                return endTime;
            case "3":
                calendar.add(calendar.DATE, 30 * 6);
                endTime = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());
                return endTime;
            case "4":
                calendar.add(calendar.DATE, 30 * 12);
                endTime = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());
                return endTime;
        }
        return "";
    }
}
